package com.jade.linkong.service.impl;
 
import com.jade.common.core.text.Convert;
import com.jade.common.utils.ChineseToEnglish;
import com.jade.common.utils.DateUtils;
import com.jade.common.utils.StringUtils;
import com.jade.framework.shiro.service.SysPasswordService;
import com.jade.framework.util.ShiroUtils;
import com.jade.linkong.domain.LkMatterProject;
import com.jade.linkong.domain.LkProject;
import com.jade.linkong.mapper.LkMatterProjectMapper;
import com.jade.linkong.mapper.LkProjectMapper;
import com.jade.linkong.service.ILkProjectService;
import com.jade.system.domain.SysUser;
import com.jade.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 项目Service业务层处理
 * 
 * @author jade
 * @date 2020-07-13
 */
@Service
public class LkProjectServiceImpl implements ILkProjectService 
{
    @Autowired
    private LkProjectMapper lkProjectMapper;
    @Autowired
    private LkMatterProjectMapper matterProjectMapper;
    @Autowired
    private SysPasswordService passwordService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询项目
     * 
     * @param proId 项目ID
     * @return 项目
     */
    @Override
    public LkProject selectLkProjectById(String proId)
    {
        return lkProjectMapper.selectLkProjectById(proId);
    }
 
    /**
     * 查询项目列表
     * 
     * @param lkProject 项目
     * @return 项目
     */
    @Override
    public List<LkProject> selectLkProjectList(LkProject lkProject)
    {
        return lkProjectMapper.selectLkProjectList(lkProject);
    }
 
    /**
     * 新增项目
     * 
     * @param lkProject 项目
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertLkProject(LkProject lkProject)
    {

        lkProject.setCreateTime(DateUtils.getNowDate());
        lkProjectMapper.insertLkProject(lkProject);
        SysUser user = new SysUser();
        user.setPhonenumber(lkProject.getLeaderPhone());
        user.setLoginName(lkProject.getLeaderPhone());
        user.setPassword(lkProject.getPassword());
        user.setRoleIds(new Long[]{3L});
        user.setUserName(lkProject.getCompanyName());
        user.setSalt(ShiroUtils.randomSalt());
        user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
        userService.insertUser(user);
        user.setCreateBy(ShiroUtils.getLoginName());
        if(StringUtils.isNotEmpty(lkProject.getProTypeName())){
            String code = getCode(lkProject.getProTypeName(), lkProject.getProId());
            lkProject.setProCode(code);
        }
        return lkProjectMapper.updateLkProject(lkProject);
    }
 
     private String getCode(String typeName, Long proId){
         if(StringUtils.isNotEmpty(typeName)){
             String num = "";
             synchronized (this){
                 num = proId < 10 ? "0" + proId:proId + "";
             }
             String s = ChineseToEnglish.getPinYinHeadChar(typeName).toUpperCase();
             String code = s + "XM-" + DateUtils.getDate2() + "-" + num;
             return code;
         }
         return null;
     }
     @Override
     public int matterCountByProject(int proId){
         return matterProjectMapper.selectCountByProjectId(proId);
     }
    /**
     * 修改项目
     * 
     * @param lkProject 项目
     * @return 结果
     */
    @Override
    public int updateLkProject(LkProject lkProject)
    {
        if(StringUtils.isNotEmpty(lkProject.getProTypeName())){
            String code = getCode(lkProject.getProTypeName(), lkProject.getProId());
            lkProject.setProCode(code);
        }
        lkProject.setUpdateTime(DateUtils.getNowDate());
        return lkProjectMapper.updateLkProject(lkProject);
    }
 
    /**
     * 删除项目对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteLkProjectByIds(String ids)
    {
        return lkProjectMapper.deleteLkProjectByIds(Convert.toStrArray(ids));
    }
 
    /**
     * 删除项目信息
     * 
     * @param proId 项目ID
     * @return 结果
     */
    @Override
    public int deleteLkProjectById(String proId)
    {
        return lkProjectMapper.deleteLkProjectById(proId);
    }
 
    @Override
    public int proNameCheck(String proName) {
 
        return lkProjectMapper.proNameCheck(proName);
    }

    @Override
    public Map<String,Object> getCount() {
        Map<String,Object> map = new HashMap<>();
//        int p_count = lkProjectMapper.getCount();
        LkMatterProject lkMatterProject = new LkMatterProject();
        int total_count = matterProjectMapper.getCount(lkMatterProject);
        lkMatterProject.setLight("1");
        int red_count = matterProjectMapper.getLightCount(lkMatterProject);
        lkMatterProject.setLight("2");
        int yel_count = matterProjectMapper.getLightCount(lkMatterProject);
        map.put("p_count", 0);
        map.put("total_count", total_count);
        map.put("red_count", red_count);
        map.put("yel_count", yel_count);
        return map;
    }

    @Override
    public SysUser selectUserByPhone(String proId) {
        return lkProjectMapper.selectUserByPhone(proId);
    }

    @Override
    public List<LkProject> selectProjectMoreMatterList() {
        LkProject project = new LkProject();
        // 根据角色使用角色id或部门id做为查询条件
        String roleKey = ShiroUtils.getSysUser().getRoleKey();
        Long id = null;
        if ( StringUtils.isNotNull(roleKey) && roleKey.equals("company")){
            id = ShiroUtils.getSysUser().getUserId();
        }else {
            id = ShiroUtils.getSysUser().getDeptId();
        }
        project.setMainOrg(id);
        project.setAcceptingState(1);
        return lkProjectMapper.selectProjectMoreMatterList(project);
    }
}